package pri.abner.practice.thread.synchronize;
/**
 * @description 
 * @author  Abner Chow
 * @date Created in 2018/11/15 16:00
 * @version v4.2.0
 */
public class SynchronizedT {


    public static void main(String[] args) {

        //模拟两个人同时银行取钱操作
        Thread a = new Thread(new People(), "boss");
        Thread b = new Thread(new People(), "账务");
        //线程启动
        a.start();
        b.start();

    }


}


class Bank {

    /**
     * 设置操作共有资源（余额）的同步锁，必须添加static，保证每一个银汉实例都是同一把同步锁
     */
    final static Object lock = new Object();

    static int blance = 10000;

    public static int getBlance() {
        return blance;
    }

    public static void setBlance(int blance) {
        Bank.blance = blance;
    }

    public void withdraw(int money) {
        //同步块，提现取钱操作
        synchronized (lock) {
            blance = blance - money;
            System.out.println(Thread.currentThread().getName() + "提现后余额：" + blance);
        }

    }

}

class People implements Runnable {

    /**
     * 取钱操作
     * @param
     * @return void
     * @author Abner Chow
     * @date 2018/11/16 16:46
     */
    @Override
    public void run() {
        Bank bank = new Bank();
        for (int i = 0; i < 10; i++) {
            bank.withdraw(500);
        }

    }
}


